home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / ISDIR.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  2KB  |  63 lines

  1. /*
  2. **  Public domain code from the FidoNet C_Echo by Martin Maney
  3. */
  4.  
  5. #include <dos.h>
  6.  
  7. /*  isDirectory -- is the path a valid directory?
  8. **
  9. **   returns non-zero if yes, zero if no (including errors)
  10. **
  11. **   This relies upon DOS's behavior in resolving a pathname: the path part is
  12. **   parsed and checked, then the filename part is checked against the device
  13. **   list before the final directory is actually searched.  This is crufty but
  14. **   seems to be deeply ingrained in MS-DOS, and works better on more DOS
  15. **   versions than any alternative I've found.
  16. **
  17. **   If you don't trust this, using "*.*" and _A_SUBDIR to search for any file
  18. **   or subdirectory should work for all cases except an empty root directory.
  19. */
  20.  
  21. int isDirectory(char const *path)
  22. {
  23.       struct find_t ft;
  24.       char buf[MAX_PATH];
  25.  
  26.       if (*path == 0 || splicePath(buf, MAX_PATH, path, "nul") < 0)
  27.             return 0;
  28.       return _dos_findfirst(buf, 0, &ft) == 0;
  29. }
  30.  
  31. #include <string.h>
  32.  
  33. /*  splicePath -- splice a pathname and a filename together
  34. **
  35. **   returns the length of the result, or negative on error (too long)
  36. **
  37. **   n is the maximum allowable length for the spliced result including NUL
  38. **
  39. **   NB: it's prototyped for non-overlapping sources and destination; it WILL
  40. **       blow up for non-trivial cases if buf == filename; this implementation
  41. **       will work with buf == path, and this is waranteed (it proved useful).
  42. */
  43.  
  44. int splicePath(char *buf, size_t n, char const *path, char const *filename)
  45. {
  46.       int pl = strlen(path);
  47.       int tl = pl + strlen(filename);
  48.       int addSep = 0;
  49.  
  50.       if (pl != 0 && path[pl-1] != ':' && path[pl-1] != '/' &&
  51.             path[pl-1] != '\\')
  52.                   addSep = 1;
  53.  
  54.       if (n < tl + addSep + 1)
  55.             return -1;
  56.       if (buf != path)                /* otherwise copy is redundant! */
  57.             strcpy(buf, path);
  58.       if (addSep)
  59.             strcat(buf, "/");
  60.       strcat(buf, filename);
  61.       return tl + addSep;
  62. }
  63.